package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.RegexUtils;

public class MydriversNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://.*.mydrivers.com/[0-9]*/[0-9]*/[0-9]*.htm",
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		Document doc=Jsoup.parse(htmltxt);
		String title=null;
		String content=null;
		Long date=null;

		title=doc.select("title").text();
		title=StringUtils.substringBefore(title, "-");
		if (StringUtils.isBlank(title)) title=doc.select("h2#thread_subject").text();

		content=HtmlCleaner.getContentHtml(url,doc.select("div.content"));
		if (StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("div.news_info"));
			if (StringUtils.isBlank(content)) {
				content=HtmlCleaner.getContentHtml(url,doc.select("div.pc_info p"));
			}
		}

		date=DateUtils.matchDate(doc.select("div.postmeta").text());
		if (date == null) date=DateUtils.matchDate(doc.select("div.news_bt1_left").text());

		String author=JSoupUtils.matchAuthor(doc, "作者:");
		news.setAuthor(author);
		news.setUrl(url);
		news.setTitle(title);
		news.setContent(content);
		news.setDate(date);
		if (StringUtils.isBlank(news.getTitle())||news.getDate() == null) {
		}
		return news;
	}

	public static void main(String[] args) {
		String url="http://news.mydrivers.com/1/290/290643.htm";
		MydriversNewsAnalyse test=new MydriversNewsAnalyse();
		url=UrlArgumentTop.FromatUrl(url);
		System.out.println(url);
		System.out.println(DoMainUtils.GetDomainName(url));

		System.err.println(test.isDetailPage(url));

		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=test.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
